package com.galeno.zoopeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.List;

/**
 * @author galeno @Title: @Description:
 * @date 2021/7/3017:36
 */
public class Demo3 {
  public static void main(String[] args) throws Exception {
    ZooKeeper zk = new ZooKeeper("galeno01:2181", 2000, null);
    // zk.delete("/aaa",-1);
    rmr("/a", zk);
    zk.close();
  }
  // 递归删除节点
  public static void rmr(String path, ZooKeeper zk) throws Exception {
    // 如果有子节点  遍历子节点 删除
    List<String> children = zk.getChildren(path, null);
    if (children != null && children.size() > 0) {
      // 遍历子节点
      for (String child : children) {
        rmr(path + "/" + child, zk);
      }
    }

    zk.delete(path, -1);
  }
}